package com.score;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Roger on 2019/6/23.
 */

public class RLyricReader {

    public static ArrayList<LyricObject> readLrc(Context context) throws Exception {
        try {
            BufferedReader reader = null;
            reader = new BufferedReader(new InputStreamReader(context.getAssets().open("十年/十年.lrc"), "utf-8"));
            String line = null;
            ArrayList<LyricObject> list = new ArrayList<>();
            int preLastTime = 0;
            while ((line = reader.readLine()) != null) {
                if (line.contains("]<")) {
                    LyricObject lrcObject = new LyricObject();
                    lrcObject.startTime = Integer.valueOf(line.substring(1, line.indexOf(",")));
//                    lrcObject.duration = Integer.valueOf(line.substring(line.indexOf(",") + 1, line.indexOf("]")));
                    if (preLastTime > lrcObject.startTime) {
                        lrcObject.startTime = preLastTime;
                    }

                    line = line.substring(line.indexOf("]") + 1);
                    String[] words = line.split("<");
                    for (int i = 1; i < words.length; i++) {
                        String[] content = words[i].split(">");
                        int wordStartTime = Integer.valueOf(content[0].substring(0, content[0].indexOf(",")));
                        int wordDuration = Integer.valueOf(
                                content[0].substring(content[0].indexOf(",") + 1, content[0].lastIndexOf(",")));
                        LyricObject.Word word = new LyricObject.Word(wordStartTime, wordDuration, content[1]);
                        lrcObject.words.add(word);
                        lrcObject.content.append(content[1]);
                        lrcObject.duration += wordDuration;
                    }
                    list.add(lrcObject);
                    preLastTime = lrcObject.startTime + lrcObject.duration;
                }
            }
            reader.close();
            System.out.println("lrc read over");
            return list;
        } catch (Exception e) {
            throw new Exception("解释歌词文件出错：" + e.getMessage());
        }
    }

    public static ArrayList<LyricObject> readLrc(String file) throws Exception {
        try {
            BufferedReader reader = null;
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file)), "utf-8"));
            String line = null;
            ArrayList<LyricObject> list = new ArrayList<>();
            int preLastTime = 0;
            while ((line = reader.readLine()) != null) {
                if (line.contains("]<")) {
                    LyricObject lrcObject = new LyricObject();
                    lrcObject.startTime = Integer.valueOf(line.substring(1, line.indexOf(",")));
                    lrcObject.duration = Integer.valueOf(line.substring(line.indexOf(",") + 1, line.indexOf("]")));
                    if (preLastTime > lrcObject.startTime) {
                        lrcObject.startTime = preLastTime;
                    }
                    preLastTime = lrcObject.startTime + lrcObject.duration;

                    line = line.substring(line.indexOf("]") + 1);
                    String[] words = line.split("<");
                    for (int i = 1; i < words.length; i++) {
                        String[] content = words[i].split(">");
                        int wordStartTime = Integer.valueOf(content[0].substring(0, content[0].indexOf(",")));
                        int wordDuration = Integer.valueOf(
                                content[0].substring(content[0].indexOf(",") + 1, content[0].lastIndexOf(",")));
                        LyricObject.Word word = new LyricObject.Word(wordStartTime, wordDuration, content[1]);
                        lrcObject.words.add(word);
                        lrcObject.content.append(content[1]);
                    }
                    list.add(lrcObject);
                }
            }
            reader.close();
            System.out.println("lrc read over");
            return list;
        } catch (Exception e) {
            throw new Exception("解释歌词文件出错：" + e.getMessage());
        }
    }

    public static class LyricObject {
        public int startTime;
        public int duration;
        public StringBuffer content;
        public ArrayList<Word> words;

        public LyricObject() {
            words = new ArrayList<>();
            content = new StringBuffer();
        }

        public static class Word {
            public int startTime;
            public int duration;
            public String word;

            public Word(int startTime, int duration, String word) {
                super();
                this.startTime = startTime;
                this.duration = duration;
                this.word = word;
            }
        }
    }

}
